package com.itheima.service.impl;

import com.alibaba.dubbo.config.annotation.Service;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.itheima.dao.MenuDao;
import com.itheima.entity.PageResult;
import com.itheima.entity.QueryPageBean;
import com.itheima.pojo.Menu;
import com.itheima.pojo.Role;

import com.itheima.service.MenuService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

@Service(interfaceClass = MenuService.class)
@Transactional
public class MenuServiceImpl implements MenuService {

    @Autowired
    private MenuDao menuDao;

    //查询所有菜单信息
    @Override
    public List<Menu> findAll() {

        return menuDao.findAll();
    }

    @Override
   /* public List<Menu> findPage(QueryPageBean queryPageBean) {*/
    public PageResult findPage(QueryPageBean queryPageBean) {

        int currentPage = queryPageBean.getCurrentPage();
        int pageSize = queryPageBean.getPageSize();
        String queryString = queryPageBean.getQueryString();

        Page<Menu> page =  new Page<>();
        if(queryString!=null && queryString.length()>0) {
            Menu menu = menuDao.findByName(queryString);
            if(menu!=null && menu.getLevel()==1){
                PageHelper.startPage(currentPage, pageSize);

                page = menuDao.findByCondition(queryString);

            }else if(menu!=null && menu.getLevel()==2){
                PageHelper.startPage(currentPage, pageSize);
                Menu pmenu = menuDao.findById(menu.getParentMenuId());
                pmenu.getChildren().add(menu);
                page.getResult().add(pmenu);

            }

        }else {
            PageHelper.startPage(currentPage, pageSize);

            page = menuDao.findByCondition(queryString);


        }

        Long total = page.getTotal();
        List rows = page.getResult();

        return new PageResult(total,rows);
       /* List<Menu> menuList = new ArrayList<>();
        List<Menu> list = menuDao.findAll();

        for (Menu menu : list) {
            if (menu.getParentMenuId() == null ) {
                menuList.add(menu);
            }
        }

        if (!menuList.isEmpty()) {
            for (Menu menu1 : menuList) {
                for (Menu menu : list) {
                    if (  menu.getParentMenuId()!=null && menu.getParentMenuId() == menu1.getId()) {
                        menu1.getChildren().add(menu);
                    }
                }
            }
        }
        *//*PageInfo<Menu> page = new PageInfo<>(menuList);*//*

        return menuList;*/
    }

    @Override
    public void add(Menu menu) {

        String name = menu.getName();
        String url = menu.getLinkUrl();



        if(url==null || url == "" || url.length()==0){
            Menu menu1 = menuDao.findByName(name);
            if (menu1 == null) {
                menuDao.add(menu);
            }else{
                throw new RuntimeException("菜单名或访问路径重复");
            }

        }else {
            Menu menu1 = menuDao.findByName(name);
            Menu menu2 = menuDao.findByUrl(url);

            if(menu1==null && menu2==null){
                menuDao.add(menu);
            }else{
                throw new RuntimeException("菜单名或访问路径重复");
            }
        }

    }

    //查找所有菜单id和菜单名
    @Override
    public List<Map<String, Object>> findAllMenuIds() {

        return menuDao.findAllMenuIds();
    }

    //根据id查找菜单
    @Override
    public Menu findById(Integer menuId) {
        return menuDao.findById(menuId);
    }

    //更新菜单信息
    @Override
    public void edit(Menu menu) {

        String name = menu.getName();
        String url = menu.getLinkUrl();

        Menu menu1 = menuDao.findByName(name);
        Menu menu2 = menuDao.findByUrl(url);

        if(menu1==null && menu2==null){

                menuDao.edit(menu);

        }
        if(menu1 == null){
            if(menu2 != null && menu2.getId() == menu.getId()){
                menuDao.edit(menu);
            }
        }
        if (menu2 == null) {
            if (menu1 != null && menu1.getId() == menu.getId()) {
                menuDao.edit(menu);
            }
        }

        throw new RuntimeException("菜单名或访问路径重复");
    }

    //删除菜单
    @Override
    public void deleteById(Integer id) {
        //检查删除id是否和检查组有关联

        List<Integer> list =  menuDao.findRoleByMenuId(id);

        if (!list.isEmpty()) {
            throw new RuntimeException("该菜单和"+list.size()+"个角色相关联");
        }else {
            Menu menu = menuDao.findById(id);
            if(menu.getLevel()==1){
                List<Menu> allMenu = menuDao.findAllMenu();
                for (Menu allMenu1 : allMenu) {
                    if ( allMenu1.getId() == menu.getId()) {
                        for (int i = 0; i < allMenu1.getChildren().size(); i++) {
                            menuDao.deleteById(allMenu1.getChildren().get(i).getId());
                        }
                    }
                    if (allMenu1.getPriority() > menu.getPriority()) {
                        allMenu1.setPriority(allMenu1.getPriority()-1);
                        allMenu1.setPath(Integer.toString(allMenu1.getPriority()));
                        for (Menu child : allMenu1.getChildren()) {
                            child.setPath("/"+allMenu1.getPath()+"-"+child.getPriority());
                            menuDao.edit(child);
                            menuDao.deleteById(menu.getId());
                        }
                    }
                    menuDao.edit(allMenu1);
                }
            }else{
                Menu byVal = menuDao.findByVal(menu.getParentMenuId());
                for (Menu child : byVal.getChildren()) {
                    if (child.getPriority() > menu.getPriority()) {
                        child.setPriority(child.getPriority()-1);
                        child.setPath("/"+byVal.getPath()+"-"+(child.getPriority()));
                        menuDao.edit(child);
                    }

                }
            }
            menuDao.deleteById(id);
        }
    }

    @Override
    public String findByVal(Integer val) {
       Menu menu = menuDao.findByVal(val);
        String a = menu.getPath();
        int b = menu.getChildren().size()+1;

        String path = "/" + a + "-" + b;
        return path;
    }
}
